def hanoi(n, source, helper, target):
#    print "hanoi( ", n, source, helper, target, " called"
    if n > 0:
        # move tower of size n - 1 to helper:
        hanoi(n - 1, source, target, helper)
        # move disk from source peg to target peg
        if source[0]:
            disk = source[0].pop()
            print ("moving " + str(disk) + " from " + source[1] + " to " + target[1])
            target[0].append(disk)
        # move tower of size n-1 from helper to target
        hanoi(n - 1, helper, source, target)
        
source = ([5,4,3,2,1], "A")
#source = ([2,1], "source")
target = ([], "C")
helper = ([], "B")
hanoi(len(source[0]),source,helper,target)

print (source, helper, target)
